// createRouter 创建router实例
/**
 * createWebHistory 创建history路由模式
 *  常见路由模式
 *    history
 *    hash
 */
import { createRouter, createWebHistory } from 'vue-router'
import Login from '@/views/Login/index.vue'
import Layout from '@/views/Layout/index.vue'
import Home from '@/views/Home/index.vue'
import Category from '@/views/Category/index.vue'
import SubCategory from '@/views/SubCategory/index.vue'
import ProductDetail from "@/views/Detail/index.vue";
import CartList from '@/views/CartList/index.vue'
import Checkout from '@/views/Checkout/index.vue'
import Pay from '@/views/Pay/index.vue'
import PayBack from '@/views/Pay/components/PayBack.vue'
import Member from '@/views/Member/index.vue'
import MemberInfo from '@/views/Member/components/UserInfo.vue'
import MemberOrder from '@/views/Member/components/UserOrder.vue'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  // 路由规则：path和component对应关系
  routes: [
    {
      name: 'layout',
      path: '/',
      component: Layout,
      children: [
        {
          // '' 默认二级路由
          path: '',
          component: Home
        },
        {
          name: 'category',
          path: 'category/:id',
          component: Category
        },
        {
          name: 'subCategory',
          path: '/category/sub/:id',
          component: SubCategory
        },
        {
          name: 'detail',
          path: '/detail/:id',
          component: ProductDetail
        },
        {
          name: 'cartList',
          path: 'cartList',
          component: CartList
        },
        {
          name: 'checkout',
          path: 'checkout',
          component: Checkout
        },
        // 支付页
        {
          name: 'pay',
          path: 'pay',
          component: Pay
        },
        // 支付结果跳转页
        {
          name: 'payBack',
          path: 'paycallback',
          component: PayBack
        },
        // 会员中心 个人中心
        {
          name: 'member',
          path: 'member',
          component: Member,
          children: [
            {
              path: '', // 默认
              component: MemberInfo
            },
            {
              path: 'order',
              component: MemberOrder
            }
          ]
        }
      ]
    },
    {
      // 登录页
      name: 'login',
      path: '/login',
      component: Login
    }
  ],
  scrollBehavior() {
    return {
      top: 0
    }
  }
})

export default router
